
#ifndef __SSH_CHAIN_H
#define __SSH_CHAIN_H

#include <apr-1/apr_pools.h>
#include "../db/ClusterSSHDB.h"
#include "../db/KeyDB.h"
#include "../db/PendingDB.h"
#include "../crypt/RSAKeyPair.h"
#include "../net/SSHClient.h"
#include "../net/ForwardServer.h"

typedef struct ssh_link_s
{
   SSHClient *scli;
   ForwardServer *fwds;
   const char *remote_hostname;
   int remote_hostport;
   const char *real_username;
   int local_hostport; // set this to zero, if the remote hostport is to be used
   const char *expected_host_fingerprint;
   const char *obtained_host_fingerprint;
   int usekey;
   int activated;
   struct ssh_link_s *next;
} SSHLink;

class SSHChain
{
   public:
      SSHChain(ClusterSSHDB *iclusters, apr_pool_t *imp);
      ~SSHChain();
      char *BuildChainToCluster(const char *clusterxmlfile, const char *clusterid, const char *isso_userid, apr_pool_t *imp);
      void DustForFingerprints(PendingDB *pdb);
      void SetAuthenticatorKeys(KeyDB *sshkeys, const char *keyname, const char *keypass);
      void SetAuthenticatorPassword(const char *authpass);
      void AddToSSHChain(const char *isso_userid, const char *hostname);
      int WasFingerprintProblem();
      char *GetFailedLinkHostName();
      SSHClient *ActivateSSHChain(int *failcode);
   private:
      apr_pool_t *mp;
      SSHLink **GetLastChainLink();
      void AddToSSHChain(const char *user, int port, const char *host, const char *fingerprint, int usekey);
      void DestroySSHChain(SSHLink *root);
      int ActivateSSHChainLink(SSHLink *tlink, int useremote);
      const char *sso_userid;
      const char *sso_keyname;
      const char *sso_keypass;
      SSHLink *root;
      ClusterSSHDB *clusterdb;
      KeyDB *keydb;
      const char *our_pass;
      int processed_for_unknown_fingerprints;
};

#endif
